package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author rookie001
 * @creat 2023-06-11-20:43
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
   @Autowired
   private RedisTemplate redisTemplate;
    @Override
    public ResponseResult listService(SensitiveDto dto) {
        //数据校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页校验
        dto.checkParam();
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置分页条件
        //按照创建时间倒叙
        lambdaQueryWrapper.orderByDesc(WmSensitive::getCreatedTime);
        //模糊查询关键字
        lambdaQueryWrapper.like(WmSensitive::getSensitives, dto.getName());
        page = page(page, lambdaQueryWrapper);
        //返回结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());//////
        return responseResult;

    }


    //删除
    @Override
    public ResponseResult delService(Integer id) {
        //数据校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //删除redis缓存
        redisTemplate.delete("Sensitive");
        //按照ID删除wm_sensitive
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //保存
    @Override
    public ResponseResult saveService(WmSensitive wmSensitive) {
        //数据校验
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //设置时间
        wmSensitive.setCreatedTime(new Date());
        //删除redis缓存
        redisTemplate.delete("Sensitive");
        //保存到wm_sensitive
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //修改
    @Override
    public ResponseResult updateService(WmSensitive wmSensitive) {
        //数据校验
        if (wmSensitive == null || wmSensitive.getSensitives().isEmpty()) {
            return ResponseResult.okResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //删除redis缓存
        redisTemplate.delete("Sensitive");
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
